<?php
require 'includes/db.php';
if (isset($_POST['submit'])) {


	$p_image = $_FILES['image']['name'];
	$p_image = date('ymdHis') . '_' . uniqid() . '_' . $p_image;
	$fileInfoArray = pathinfo($p_image);
	$src = $_FILES['image']['tmp_name'];
	$dest = "images/project/$p_image";
	$isMoved = @move_uploaded_file($src, $dest);
	$_POST['image'] = $p_image;
}

require 'header.php';
?>
<script src="js/jquery-1.8.2.min.js" type="text/javascript"></script>
<script src="js/jquery.validate.js" type="text/javascript"></script>
<script src="js/register.jquery.js" type="text/javascript"></script>
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
	
	<h3>Latest Projects</h3>
    <form id="registerId" action="" method="post" enctype="multipart/form-data">
        <table class="table table-striped">
            <tbody>
                <tr>
                    <td width="20%" align="right">Name :</td>
                    <td width="40%" align="right">
                        <input class="form-control" type="text" name="name" value="" />
                    </td>
                    <td width="40%" align="right"><label for="name" generated="true" class="error"></label></td>
                </tr>
                <tr>
                    <td align="right">Upload Image :</td>
                    <td >
                        <input class="form-control" type="file" name="image" value="" placeholder="My avatar is..." />       </td>
                    <td><label for="image" generated="true" class="error"></label></td>
                </tr>
                <tr>
                    <td width="20%" align="right">Description :</td>
                    <td width="40%" align="right">
                        <input class="form-control" type="text" name="description" value="" />
                    </td>
                    <td width="40%" align="right"><label for="description" generated="true" class="error"></label></td>
                </tr>
                <tr>
                    <td width="20%" align="right">URL :</td>
                    <td width="40%" align="right">
                        <input class="form-control" type="text" name="href" value="" />
                    </td>
                    <td width="40%" align="right"><label for="href" generated="true" class="error"></label></td>
                </tr>
                 <tr>
                    <td width="20%" align="right">Project Date :</td>
                    <td width="40%" align="right">
                        <input class="form-control" type="date" name="date" value="" />
                    </td>
                    <td width="40%" align="right"><label for="date" generated="true" class="error"></label></td>
                </tr>
                <tr>
                    <td align="right">&nbsp;</td>
                    <td >
                        <input class="form-control" type="submit" name="submit" value="Upload Image" />        </td>
                    <td align="right">&nbsp;</td>
                </tr>

            </tbody>

        </table>
    </form>

<?php
$tableName = 'projects';
	if(isset($_POST['submit'])){
	$_POST = array_map('trim',$_POST);
	unset($_POST['submit']);
	
	foreach ($_POST as $key => $value) { 
        $tmp_col[] = $key; 
        $tmp_dat[] = "'".mysql_real_escape_string($value)."'"; // <-- escape against SQL injections
    } 
    $columns = join(',', $tmp_col); 
    $data = join(',', $tmp_dat);
    // Create and execute SQL command 
    $sql = 'INSERT INTO '.$tableName.'('.$columns.')VALUES('. $data.')'; 
    $result = mysql_query($sql) or die(mysql_error()); 
	echo success("Success Fully Added");	
}




$where = '';
$id = isset($_GET['id']) ? $_GET['id'] : 0;
$total_users = CountAll('projects');


########### Count total records in users table ##############
########### PAGING START ###########################

$rpp = 4;
// Record Per Page (offset)
$total_pages = ceil($total_users / $rpp);
$currentPage = isset($_GET['page']) ? $_GET['page'] : 1;
$index = $rpp * ($currentPage - 1);
// limit or from where we have to start fetching
?>

<?php
if (isset($_GET['mode'])) {
	$query = mysql_query("select image from projects where id='$id'");
	$image_name = mysql_fetch_array($query);
	$deleteFile = $image_name['image'];
	@unlink('images/project/' . $deleteFile);
	$numRows = deleteby($tableName, $id);
	if ($numRows == 1) {
		echo success("Succefully Deleted");
	}
}
if (isset($_GET['status'])) {
	$newStatus = $_GET['status'] == 0 ? 1 : 0;
	$data = array('status' => $newStatus);
	$flag = updateById($tableName, $data, $id);
	echo success("Successfully Upadated");
}
?>
    <table class="table table-striped">
        <thead>
            <tr>
                <th width="10%" align="right">Name</th>
                <th width="10%" align="right">Image</th>
                <th width="20%" align="right">Uploaded Date</th>
                <th width="20%" align="right">URL</th>
                <th width="10%" align="right">Status</th>
                <th width="10%" align="right">Action</th>
            </tr>
        </thead>

<?php
$sql = fetchAll($tableName, '', 'id', $index, $rpp);
while ($data = mysql_fetch_assoc($sql)) {
    $id = $data['id'];

    $status = getStatus($data['status']);
    $status = "<a style=\"text-decoration:none\" href=\"?id=$id&status=$data[status]&page=$currentPage\">$status </a>";
    ?>				<tbody>
                <tr>

                    <td><?= $data['name'] ?></td>
                    <td align="center"><img height="30" width="90" src="images/project/<?=$data['image'] ?>"><br/>
                        <a target="_blank" href="images/project/<?= $data['image'] ?>" >View in Full Size</a>
                    </td>
                    <td><?= $data['date'] ?></td>
                     <td><?= $data['href'] ?></td>
                </td><td align="center"><?= $status ?></td>

            <td><div><a onClick="return confirm('Are you sure to delete?')" href="?mode=del&id=<?= $data['id'] ?>&page=<?= $currentPage ?>" class="delete"><span class="glyphicon glyphicon-trash" style="font-size:30px; color: red;"></span></a>&nbsp;&nbsp;&nbsp;</div>
                <br>

            </td>


<?php } ?>
</table>
<ul class="pagination pagination-lg">

    <li class="disabled"><a href="#">&laquo;</a></li>

<?php
for ($i = 1; $i <= $total_pages; $i++) {
    if ($i == $currentPage) {
        ?>
            <li class="active"><a href="#"><?= $i ?> <span class="sr-only">(current)</span></a></li>

            <?php
			} else { //end current page if()
            ?>
            <li><a href="?page=<?= $i ?>"><?= $i ?></a></li>
            <?php
			} // end else
			}//end for() for paging
    ?>

    <li><a href="#">&raquo;</a></li>
</ul>
</div>
